package org.zhixiang.gisQuery.query;

import java.util.List;

import org.zhixiang.gisQuery.rtree.LinePoint;

/**
 * 
 * @say little Boy, don't be sad.
 * @name Rezar
 * @time 2018年9月14日 上午11:11:27
 * @Desc 些年若许,不负芳华.
 *
 */
public class InBoundChecker {

	public static int useTime = 0;

	public static boolean checkInBoundWithLine(List<LinePoint> linePoints, double checkX, double checkY) {
		boolean flag = false;
		double px = checkX, py = checkY;
		for (LinePoint line : linePoints) {
			double sx = line.getFirst().x, sy = line.getFirst().y, tx = line.getSecond().x, ty = line.getSecond().y;
			// 点与多边形顶点重合
			if ((sx == px && sy == py) || (tx == px && ty == py)) {
				return true;
			}
			// 判断线段两端点是否在射线两侧
			if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
				// 线段上与射线 Y 坐标相同的点的 X 坐标
				double x = sx + (py - sy) * (tx - sx) / (ty - sy);
				// 点在多边形的边上
				if (x == px) {
					return true;
				}
				// 射线穿过多边形的边界
				if (x > px) {
					flag = !flag;
				}
			}

		}
		return flag;
	}

	public static boolean checkInBound(List<SelfPoint> linePoints, double checkX, double checkY) {
		boolean flag = false;
		double px = checkX, py = checkY;
		for (int i = 0, l = linePoints.size(), j = l - 1; i < l; j = i, i++) {
			double sx = linePoints.get(i).x, sy = linePoints.get(i).y, tx = linePoints.get(j).x,
					ty = linePoints.get(j).y;
			// 点与多边形顶点重合
			if ((sx == px && sy == py) || (tx == px && ty == py)) {
				return true;
			}
			// 判断线段两端点是否在射线两侧
			if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
				// 线段上与射线 Y 坐标相同的点的 X 坐标
				double x = sx + (py - sy) * (tx - sx) / (ty - sy);
				// 点在多边形的边上
				if (x == px) {
					return true;
				}
				// 射线穿过多边形的边界
				if (x > px) {
					flag = !flag;
				}
			}
		}
		return flag;
	}

	public static boolean checkInBound(List<SelfPoint> linePoints, SelfPoint needCheck) {
		boolean flag = false;
		double px = needCheck.x, py = needCheck.y;
		for (int i = 0, l = linePoints.size(), j = l - 1; i < l; j = i, i++) {
			double sx = linePoints.get(i).x, sy = linePoints.get(i).y, tx = linePoints.get(j).x,
					ty = linePoints.get(j).y;
			// 点与多边形顶点重合
			if ((sx == px && sy == py) || (tx == px && ty == py)) {
				return true;
			}
			// 判断线段两端点是否在射线两侧
			if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
				// 线段上与射线 Y 坐标相同的点的 X 坐标
				double x = sx + (py - sy) * (tx - sx) / (ty - sy);
				// 点在多边形的边上
				if (x == px) {
					return true;
				}
				// 射线穿过多边形的边界
				if (x > px) {
					flag = !flag;
				}
			}
		}
		return flag;
	}

}
